package com.jbit.hxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.mapper.*;
import com.jbit.hxy.model.dto.SetmealQueryDTO;
import com.jbit.hxy.model.po.*;
import com.jbit.hxy.model.vo.*;
import com.jbit.hxy.service.menu.WxSetmealService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
* @author 28575
* @description 针对表【wx_setmeal(套餐)】的数据库操作Service实现
* @createDate 2024-09-08 10:16:27
*/
@Service
@RequiredArgsConstructor
public class WxSetmealServiceImpl extends ServiceImpl<WxSetmealMapper, WxSetmeal>
    implements WxSetmealService {
    private final WxSetmealMapper wxSetmealMapper;
    private final WxSetmealDishMapper wxSetmealDishMapper;
    private final DishFlavorMapper dishFlavorMapper;
    private final WxDishFlavorMapper wxDishFlavorMapper;
    private final WxDishMapper wxDishMapper;
    @Override
    public Result getSetmeal(SetmealQueryDTO setmealQueryDTO) {
        //使用Lambda表达式【指定需要查询的字段】
        IPage<WxSetmeal> page = new Page<>(setmealQueryDTO.getCurPage(),
                setmealQueryDTO.getPageSize());
        //2.设置查询条件
        QueryWrapper<WxSetmeal> queryWrapper = new QueryWrapper<>();
        //用户姓名
        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        setmealQueryDTO.getName()),
                WxSetmeal::getName,
                setmealQueryDTO.getName());



        //查询指定字段
        //queryWrapper.select("id", "title");
        //排除字段查询
//        queryWrapper.select(WxUser.class, info -> !info.getColumn().equals("deleted"));
        //3.查询
        IPage<WxSetmeal> pageResult = wxSetmealMapper.selectPage(page, queryWrapper);


        //4.封装，返回
        PageVO pageVo = new PageVO<>();

        pageVo.setTotal(pageResult.getTotal());
        pageVo.setList(pageResult.getRecords());

        pageVo.setPageSize(pageResult.getSize());
        pageVo.setCurrentPage(pageResult.getCurrent());


        return Result.success(pageVo);
    }

    @Override
    public Result addSetmeal(WxSetmeal wxSetmeal) {

        wxSetmealMapper.insert(wxSetmeal);
        return Result.success("添加成功");
    }

    @Override
    public Result updateSetmeal(WxSetmeal wxSetmeal) {
        wxSetmealMapper.updateById(wxSetmeal);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteSetmeal(Integer id) {
        wxSetmealMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result getSetmealDetail(Integer id) {
        WxSetmeal wxSetmeal = wxSetmealMapper.selectById(id);
        SetmealDetailVO setmealDetailVO = new SetmealDetailVO();
        BeanUtils.copyProperties(wxSetmeal, setmealDetailVO);
        List<WxSetmealDish> wxSetmealDishList=wxSetmealDishMapper.getSetmealDishBySetmealId(id);
        ArrayList<DishDetailVO> dishDetailVOS = new ArrayList<>();
        for (WxSetmealDish wxSetmealDish : wxSetmealDishList) {
            WxDish wxDish = wxDishMapper.selectById(wxSetmealDish.getDishId());
            DishDetailVO dishDetailVO = new DishDetailVO();
            BeanUtils.copyProperties(wxDish,dishDetailVO);
            dishDetailVO.setNum(wxSetmealDish.getCopies());
            List<Integer> flavorIds = dishFlavorMapper.getFlavorIdByDishId(wxSetmealDish.getDishId());
            ArrayList<WxDishFlavorVO> wxDishFlavorVOS = new ArrayList<>();
            for (Integer flavorId : flavorIds) {
                WxDishFlavor wxDishFlavor = wxDishFlavorMapper.selectFlavorById(flavorId);
                String values = wxDishFlavor.getValues();
                Gson gson = new Gson();
                Type listType = new TypeToken<List<String>>() {}.getType();
                List<String> sugarLevels = gson.fromJson(values, listType);
                WxDishFlavorVO wxDishFlavorVO = new WxDishFlavorVO();
                BeanUtils.copyProperties(wxDishFlavor,wxDishFlavorVO);
                wxDishFlavorVO.setValues(sugarLevels);

                wxDishFlavorVOS.add(wxDishFlavorVO);


            }
            dishDetailVO.setWxDishFlavors(wxDishFlavorVOS);
            dishDetailVOS.add(dishDetailVO);
        }
        setmealDetailVO.setDishDetailVOList(dishDetailVOS);
        return Result.success(setmealDetailVO);

    }

    @Override
    public Result getDishCountBySetmealId(Integer id) {
        Integer count = wxSetmealDishMapper.getDishCountBySetmealId(id);
        return Result.success(count);

    }

    @Override
    public Result getDishDetailBySetmealId(Integer id) {
        List<WxSetmealDish> wxSetmealDishList=wxSetmealDishMapper.getSetmealDishBySetmealId(id);
        for (WxSetmealDish wxSetmealDish : wxSetmealDishList) {
            WxSetmealFlavorVO wxSetmealFlavorVO = new WxSetmealFlavorVO();
            WxDish wxDish = wxDishMapper.selectById(wxSetmealDish.getDishId());
            List<Integer> flavorIdByDishId = dishFlavorMapper.getFlavorIdByDishId(wxDish.getId());
            for (Integer flavorId : flavorIdByDishId) {
                WxDishFlavor wxDishFlavor = wxDishFlavorMapper.selectById(flavorId);
            }

        }
        return Result.success(wxSetmealDishList);
    }

    @Override
    public Result getSetmealNameById(Integer id) {
        WxSetmeal wxSetmeal = wxSetmealMapper.selectById(id);

        return Result.success(wxSetmeal.getName());
    }

    @Override
    public Result getSetmealDishBySetmealId(Integer id) {
        return Result.success(wxSetmealDishMapper.getSetmealDishBySetmealId(id));
    }
}




